package com.longway.core.toolbox.apidoc.models;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author ryc
 */
public class ApiModule {
	
	private String title;
	private String des;
	private Map<String,ApiDocModel> apiModels = new HashMap<String,ApiDocModel>();
	
	public ApiModule(String title, String des) {
		super();
		this.title = title;
		this.des = des;
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDes() {
		return des;
	}
	public void setDes(String des) {
		this.des = des;
	}
	//按模块-controller-method格式返回
	public Map<String,List<ApiDocModel>> getApiModels() {
		List<ApiDocModel> docs = apiModels.values().stream().collect(Collectors.toList());
		Map<String,List<ApiDocModel>> docsmodules = new HashMap<String, List<ApiDocModel>>();
		for(ApiDocModel doc : docs){
			String moduleName = doc.getModule();
			if( docsmodules.get(moduleName) == null ){
				docsmodules.put(moduleName, new ArrayList<ApiDocModel>());
			}
			List<ApiDocModel> modules = docsmodules.get(moduleName);
			modules.add(doc);
		}
		return docsmodules;
	}

	public ApiModule addApiDocModel(String controller,ApiDocModel apiDocModel){
		if(apiModels.get(controller) == null){
			apiModels.put(controller,apiDocModel);
		}
		return this;
	}
	
	public ApiDocModel getControllerModel(String controllerkey) {
		return apiModels.get(controllerkey);
	}
	
	public ApiMethodModel getActionModel(String controllerkey ,String actionKey) {
		 ApiDocModel doc = apiModels.get(controllerkey);
		 for(ApiMethodModel method : doc.getMethodsModel()){
			 if(actionKey.equals(method.getActionKey())){
				 return method;
			 }
		 }
		 return null;
		 
	}
	
}
